package com.ejlchina.tool.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.ItemFirmwareUpgradeDeviceBinding
import com.huyuhui.fastble.data.BleDevice

@SuppressLint("NotifyDataSetChanged")
class PushRodParametersAdapter2(private val dataList: List<BleDevice>, private val itemClick: (MutableList<BleDevice>) -> Unit): RecyclerView.Adapter<PushRodParametersAdapter2.PushRodParametersViewHolder>() {
    private var isMultiSelect = false
    private val selectDeviceList = mutableListOf<BleDevice>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PushRodParametersViewHolder {
        val binding = ItemFirmwareUpgradeDeviceBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return PushRodParametersViewHolder(binding,itemClick)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: PushRodParametersViewHolder, position: Int) {
        holder.bind(dataList[position],position)
    }

    fun setSelectMode(selectMode: Boolean){
        isMultiSelect = selectMode
        if (!isMultiSelect && dataList.isNotEmpty()){
            selectDeviceList.clear()
            selectDeviceList.add(dataList[0])
        }
        notifyDataSetChanged()
    }
    fun getSelectDeviceList() = selectDeviceList

    fun setSelectDeviceList(list: MutableList<BleDevice>){
        selectDeviceList.apply {
            clear()
            addAll(list)
        }
        notifyDataSetChanged()
    }

    inner class PushRodParametersViewHolder(private val binding: ItemFirmwareUpgradeDeviceBinding, private val itemClick: (MutableList<BleDevice>) -> Unit): RecyclerView.ViewHolder(binding.root){
        @SuppressLint("SetTextI18n")
        fun bind(bleDevice: BleDevice, position: Int){
            binding.tvIndex.text = "${position + 1}#"
            setSelectBg(selectDeviceList.contains(bleDevice))
            if (selectDeviceList.contains(bleDevice) && !isMultiSelect){
                itemClick(selectDeviceList)
            }

            binding.root.setOnClickListener {
                if (isMultiSelect){
                    if (selectDeviceList.contains(bleDevice) && selectDeviceList.size > 1){
                        selectDeviceList.remove(bleDevice)
                    }else {
                        selectDeviceList.add(bleDevice)
                    }
                }else {
                    selectDeviceList.clear()
                    selectDeviceList.add(bleDevice)
                }
                notifyDataSetChanged()
                itemClick(selectDeviceList)
            }
        }

        private fun setSelectBg(isSelect: Boolean){
            val bgRes = if (isSelect) R.drawable.shape_co_8_so_f7f7f7_st_1f39ff else R.drawable.shape_co_8_so_ff3b30
            binding.root.setBackgroundResource(bgRes)
        }
    }
}